/**
 * 视频素材模块
 */
define( [
	'jquery',
	'bigwe_utils',
	'api',
	'../../modules/createEditor',
	'../../lib/utils',
	'constants',
	'videoUploader'
], function( $, bu, BIGWE_API, editorObj, ut, c, vu ) {

	var returnParam = {};

	var videoself = $( '.content-list .video-div' ),
		resourceVideo = $( '.video-resource', videoself ),
		myVideo = $( '.video-upload', videoself );

	returnParam.videoInit = function() {
		if ( !video[ 2 ][ 'firstVidLoad0' ] ) {
			video[ 2 ].ajaxRequest();
			video[ 2 ][ 'firstVidLoad0' ] = !0;
		}
	};
  // 视频上传
	vu.videoUploadCallback = {
		doSave: function( param, param2 ) {
			var myVideo = video[ 2 ];
			myVideo.resetPage2 = 0;
			myVideo.ajaxRequest();

			insertVideoHtml( {
				video_id: param2.video_id,
				title: param2.title
			} );
		}
	}

	videoself.show();
	$( '[event-columns="video"]' ).bwColumns( [ /*'素材视频', '我的收藏', */ '我的上传' ], function( index ) {
		var index = index.index();
		$( '.video-detail>div' ).hide();
		$( '.video-detail>div' ).eq( index ).show();

		if ( !video[ index ][ 'firstVidLoad' + index ] ) {
			video[ index ].ajaxRequest();
			video[ index ][ 'firstVidLoad' + index ] = !0;
		}
	} );
	videoself.hide();

	videoself.on( 'click', '.video-det-div', function() {

		insertVideoHtml( {
			video_id: $( this ).attr( 'video_id' ),
			title: $( this ).attr( 'video_title' )
		} );
	} );

	function insertVideoHtml( video ) {
		var html = '<section class="135editor">';
		html += '<section style="text-align:center;">';
		html += '<iframe class="bw_video_iframe" width="100%" allowfullscreen scrolling="no" style="border: none" src="/video_preview/frmPrev?vid=' + video.video_id + '"></iframe>';
		html += '</section>';
		html += '<p class="135brush" data-brushtype="text" style="text-align:center;">' + ( video.title || '视频标题' ) + '</p>';
		html += '</section>';
		return editorObj.execCommand( 'inserthtml', html );
	}

	var video = [ {}, // 素材饰品
		{}, //收藏视频
		{
			searchParam: {
				page_num: 1,
				key: '',
				l: 10,
				done: 'videos'
			},
			ajaxRequest: function( option ) {
				var self = this;
				$.extend( self.searchParam, option );
				$( '.spinner', myVideo ).show();
				$.ajax( {
					url: BIGWE_API.COMMON.GET_MATERIAL_MPNEWS,
					data: self.searchParam,
					type: 'get',
					success: function( data ) {
						data = typeof data == 'string' ? $.parseJSON( data ) : data;
						if ( data.ret ) {
							ShowTitleMessage.showWarning( data.msg, showTime );
							return;
						}
						if ( !self.resetPage2 ) {
							self.pageChangeRander( data.data.total_num, self.searchParam.page_num );
							self.resetPage2 = !0;
						}
						if ( self.searchParam.page_num == 1 ) {
							if ( !data.data.videos || !data.data.videos.length ) {
								$( '.nodatatips', myVideo ).show();
							} else {
								$( '.nodatatips', myVideo ).hide();
							}
						}
						self.total = data.data.total;
						$( '.view_scroll', myVideo ).scrollTop( 0 );
						$( '.video_list', myVideo ).html( self.getHtml( data.data.videos ) );
						$( '.spinner', myVideo ).hide();
					}
				} );
			},
			getHtml: function( arr ) {
				var html = '';

				for ( i in arr ) {
					var video = arr[ i ];
					html += '<div class="video-det-div" video_id=' + video.video_id + ' video_title="' + ( video.title || '' ) + '">';
					html += '<img class="preview_img" src="' + video.address1 + '" onerror="this.src=\'' + c.resPath + '/images/wuyulantu.png\'">';
					html += '<p class="v_title">' + ( video.title || '' ) + '</p>';
					html += '<img src="/Public/static/images/play.png" class="play_btn"/>';
					html += '</div>';
				}
				return html;
			},
			pageChangeRander: function( total, currentPage ) {
				var self = this,
					pageObj = $( ".pageControl", myVideo ),
					totalPage = parseInt( total / self.searchParam.l );

				if ( total % self.searchParam.l > 0 ) {
					++totalPage;
				}
				if ( totalPage < 2 ) {
					pageObj.hide();
				} else {
					pageObj.show();
				}

				pageObj.bwPagination( {
					total: total,
					pageCount: self.searchParam.l,
					currentPage: currentPage,
					callback: function( index ) {
						self.ajaxRequest( {
							page_num: index
						} );
					}
				} );
			}
		}
	];

	return returnParam;
} );